import { ss } from '@/utils/storage'
import { LANGUAGES } from '@/utils/const'
const LOCAL_NAME = 'appSetting'

export type Theme = 'light' | 'dark' | 'auto'

export type Language = 
'zh-cn' | 
'zh-tw' | 
'en-us' | 
'en-gb' |
'hi-in' | 
'it-it' |
'uk-ua' |
'ko-kr' |
'pt-pt' |
'fil-ph' |
'th-th' |
'ja-jp' |
'id-id' |
'fr-fr' |
'ar-sa' |
'vi-vn' |
'ru-ru' |
'tr-tr' |
'es-es'

export interface AppState {
  siderCollapsed: boolean
  theme: Theme
  language: Language
}

export function defaultSetting(): AppState {
  let defaultLocale = '';
  let lang = navigator.language.toLowerCase();
  let langs = LANGUAGES.map(o => o.key);
  if(lang.length <=2) {
    defaultLocale = langs.find(k => k.startsWith(lang)) as string;
  }else {
    defaultLocale = lang;
  }

  return {
    siderCollapsed: false, 
    theme: 'light', 
    language: defaultLocale as Language 
  }
}

export function getLocalSetting(): AppState {
  const localSetting: AppState | undefined = ss.get(LOCAL_NAME)
  return { ...defaultSetting(), ...localSetting }
}

export function setLocalSetting(setting: AppState): void {
  ss.set(LOCAL_NAME, setting)
}
